package com.imooc.user.thrift;

import com.imooc.thrift.user.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFastFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * @author liuhuan
 * @date 2018/11/1 16:33
 * @description
 */
@Configuration
@Slf4j
public class ThriftServer {


    @Autowired
    private UserService.Iface userService;

    @Value("${service.port}")
    private int servicePort;

    @PostConstruct
    public void start(){
        TProcessor processor=new UserService.Processor<>(userService);

        TNonblockingServerSocket serverSocket=null;
        try {
            serverSocket=new TNonblockingServerSocket(servicePort);

        } catch (TTransportException e) {
            log.error(e.getMessage(),e);
        }

        TNonblockingServer.Args args=new TNonblockingServer.Args(serverSocket)
                .processor(processor)
                .transportFactory(new TFastFramedTransport.Factory())
                .protocolFactory(new TBinaryProtocol.Factory());
        TServer server=new TNonblockingServer(args);
        server.serve();
    }
}
